package com.example;

import com.example.utils.ConnectionFactoryUtils;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author wangxiyuan
 * @description
 * @createDate 2021/3/29 19:40
 **/
public class DelayQueueConsumer {
    // 队列名称
    private final static String QUEUE_NAME = "delay_queue";
    private final static String EXCHANGE_NAME="delay_exchange";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = ConnectionFactoryUtils.getConnectionFactory();
        Connection connection = factory.newConnection(); Channel channel = connection.createChannel();
        channel.queueDeclare(QUEUE_NAME, true,false,false,null);
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "xxxx");
        SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        channel.basicConsume(QUEUE_NAME, true,(consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), "UTF-8");
            System.out.println(" [x] Received '" + message + sf.format(new Date()) + "'");
        },consumerTag -> {});
    }
}
